{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "view-in-github"
   },
   "source": [
    "<a href=\"https://colab.research.google.com/github/vitaldb/examples/blob/master/xgb_mortality.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Machine Learning Model for Mortality Rate Prediction\n",
    "In this example, we will learn how to build a model for predicting mortality rate with <code>xgboost</code> and <code>VitalDB</code> dataset.\n",
    "\n",
    "> Note that <b>all users who use Vital DB, an open biosignal dataset, must agree to the Data Use Agreement below. \n",
    "</b> If you do not agree, please close this window. \n",
    "Click here: [Data Use Agreement](https://vitaldb.net/dataset/?query=overview&documentId=13qqajnNZzkN7NZ9aXnaQ-47NWy7kx-a6gbrcEsi-gak&sectionId=h.vcpgs1yemdb5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "vKrrZHp_9oqc",
    "outputId": "17b28cfc-98d5-4409-cfd0-0d12a6bae607"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['age', 'sex', 'height', 'weight', 'bmi', 'asa', 'emop', 'preop_htn',\n",
       "       'preop_dm', 'preop_hb', 'preop_plt', 'preop_pt', 'preop_aptt',\n",
       "       'preop_na', 'preop_k', 'preop_gluc', 'preop_alb', 'preop_ast',\n",
       "       'preop_alt', 'preop_bun', 'preop_cr', 'preop_ph', 'preop_hco3',\n",
       "       'preop_be', 'preop_pao2', 'preop_paco2', 'preop_sao2', 'intraop_ebl',\n",
       "       'intraop_uo', 'intraop_rbc', 'intraop_ffp', 'intraop_crystalloid',\n",
       "       'intraop_colloid', 'intraop_ppf', 'intraop_mdz', 'intraop_ftn',\n",
       "       'intraop_rocu', 'intraop_vecu', 'intraop_eph', 'intraop_phe',\n",
       "       'intraop_epi', 'intraop_ca'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "!pip install -q xgboost\n",
    "import xgboost as xgb\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import confusion_matrix, accuracy_score, roc_curve, roc_auc_score, f1_score, precision_recall_curve, auc\n",
    "\n",
    "# Load datasets\n",
    "df = pd.read_csv('https://api.vitaldb.net/cases')\n",
    "df['sex'] = (df['sex'] == 'M')\n",
    "\n",
    "# Remove categorical variables\n",
    "df = df.drop(['department','optype', 'dx', 'opname', 'approach', 'position', 'ane_type', 'cormack', 'airway', 'tubesize', 'dltubesize', 'lmasize', 'lmasize', 'preop_ecg', 'preop_pft', 'iv1', 'iv2', 'aline1', 'aline2', 'cline1', 'cline2'], axis=1)\n",
    "df = df.astype(float)\n",
    "\n",
    "# Remove outcome variables\n",
    "y = df['death_inhosp'].values.flatten()\n",
    "df = df.drop(['death_inhosp','caseid','subjectid','icu_days','casestart','caseend','anestart','aneend','opstart','opend','adm','dis'], axis=1)\n",
    "\n",
    "# Input variables\n",
    "x = df.values\n",
    "\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 392
    },
    "id": "AppFFIg_3c6j",
    "outputId": "96f03d24-cc2f-45ae-9920-43ad1a24612a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4791 (event 0.9%) training, 1597 testing (event 0.9 %) samples\n",
      "optimal thval: 0.022829512134194374\n",
      "auroc: 0.808, auprc: 0.566\tacc: 0.971\tf1: 0.277\tTN 1541\tfp 42\tfn 5\tTP 9\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAVIAAAFBCAYAAAAllyfaAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwg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BkyB7ejhbY99vAG6J/ZwF1Imt+y5QK/b6TcCtxXzeaGKPjgIXAh8RDAQyD6hFMHzbAuAY4ALg0ULb1ot9n0rQ+tubqdA6ezKeB4yJ/VydYOSfmsAVwG9ir+8PzAJaFJNza6Hjex7oFVuuC1SL/Xwm8ELs51HA/YW2/yMwPPZzfYLn8WtF/fvWV/m+kvIRUUlKO9y9054FM8sG/mhmpxA8BtmU4LnrtYW2mQk8EVt3krvPMbNTgfbAB7FHX6sTtOSKc7eZ/QZYTzAC1hnASx4MBoKZvQh0B94A7jGzuwhOB7xXjuN6HbjPzPYHegHvuvuO2OmEo+370fvrAW2AZUW2r2lmcwie354N/KvQ+mPMrA3B6EHZJXx+T+AcM7sxtlyDYJyFReU4BomYCqlU1DCC0dG7uHuumS0nKAJ7ufu7sULbF3jazO4GNgH/cvchcXzGL9x94p4FMzuzuJXc/Qsz60LwjPQdZvZPd//feA7C3Xea2VSCoeEGAeP3fBxwnbu/WcYudrh7JzOrB7xKMCvufQTPo//b3c+LXZibWsL2Blzg7p/Hk1eSk86RSkXVA9bFimgP4LCiK5jZYbF1HgUeJ5jiYTpwkpntOed5gJkdEednvgucG9umFkG3/D0zOwTY7u7PAPfEPqeo3FjLuDgTCAan6E4wWAex71ft2cbMjoh9ZrHcfTPwU+DG2Db1CEZFgqA7v8cWglMce7wJXGex5rmZHVPSZ0jyUiGVihoLdDWzWQSt08+KWec0YI6ZfUJwHvNv7r6eoLCMN7NPCQpru3g+0N0/Jjh3OoPgnOlj7v4JcBQwI9bFvgW4vZjNHwE+3XOxqYh/Eszl85YH02tAMA7sQuBjCyZKe5gyenCxLHOBwcCfCFrHHxCcP93j30D7PRebCFqu2bFs82PLkmJ0+5OISCWpRSoiUkkqpCIilaRCKiJSSSqkIiKVpEIqIlJJKqQiIpWkQioiUkkqpCIilfT/EtNmYI6y6mkAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 360x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "nsamp = len(x)\n",
    "\n",
    "# Split the dataset into train and test sets.\n",
    "ntest = int(nsamp * 0.25)\n",
    "ntrain = nsamp - ntest\n",
    "x_test = x[-ntest:, :]\n",
    "y_test = y[-ntest:]\n",
    "x_train = x[:-ntest, :]\n",
    "y_train = y[:-ntest]\n",
    "print('{} (event {:.1f}%) training, {} testing (event {:.1f} %) samples'.format(ntrain, np.mean(y_train) * 100 , ntest, np.mean(y_test) * 100))\n",
    "\n",
    "# Build and fit the model\n",
    "model = xgb.XGBClassifier(objective=\"binary:logistic\", n_estimators=50, max_depth=3).fit(x_train, y_train)\n",
    "\n",
    "# Predict the data\n",
    "y_pred = model.predict_proba(x_test)[:, 1].ravel()\n",
    "\n",
    "# Test the final model\n",
    "fpr, tpr, thvals = roc_curve(y_test, y_pred)\n",
    "auroc = auc(fpr, tpr)\n",
    "precision, recall, _ = precision_recall_curve(y_test, y_pred)\n",
    "auprc = auc(recall, precision)\n",
    "optimal_idx = np.argmax(tpr - fpr)\n",
    "thval = thvals[optimal_idx]\n",
    "\n",
    "print('optimal thval: {}'.format(thval))\n",
    "y_pred = y_pred > thval\n",
    "f1 = f1_score(y_test, y_pred)\n",
    "acc = accuracy_score(y_test, y_pred)\n",
    "tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()\n",
    "print('auroc: {:.3f}, auprc: {:.3f}\\tacc: {:.3f}\\tf1: {:.3f}\\tTN {}\\tfp {}\\tfn {}\\tTP {}'.format(auroc, auprc, acc, f1, tn, fp, fn, tp))\n",
    "\n",
    "# Draw a graph\n",
    "plt.figure(figsize=(5, 5))\n",
    "plt.plot(fpr, tpr, label='AUROC = {:0.3f}'.format(auroc))\n",
    "plt.plot([0, 1], [0, 1], lw=1, linestyle='--')\n",
    "plt.xlim([0, 1])\n",
    "plt.ylim([0, 1])\n",
    "plt.legend()\n",
    "plt.xlabel('False Positive Rate')\n",
    "plt.ylabel('True Positive Rate')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "authorship_tag": "ABX9TyNhGWsWVgi4AVcJg7UxQn4T",
   "collapsed_sections": [],
   "include_colab_link": true,
   "name": "xgb_mortality.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
